﻿@using RMS.EF.Model
@model RMS_Menu
@{
    var programs_Select = ViewBag.Programs_Select as List<SelectListItem>;
    var errorMsg = ViewBag.ErrorMsg as string;
    if (!string.IsNullOrWhiteSpace(errorMsg))
    {
        <script>
            toastr.error("@(errorMsg)");
        </script>
    }
}

<link href="~/bower_components/bootstrap_dropdowns_enhancement-master/dropdowns-enhancement.min.css" rel="stylesheet" />
<div class="form-horizontal">

    @Html.HiddenFor(model => model.Id)
    <div class="form-group">
        @Html.LabelFor(model => model.ProgramId, "程序", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.ProgramId, programs_Select, new { @class = "form-control" ,onchange="programChange()"})
            @Html.ValidationMessageFor(model => model.ProgramId)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ParentId, "父节点", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div id="divPid" class="dropdown">
                <button type="button" class="btn btn-primary" data-toggle="dropdown">
                    <lable id="BtnName" class="showbtn"></lable> <span class="caret"></span>
                </button>
                <ul class="dropdown-menu"></ul>
            </div>
            @Html.HiddenFor(model => model.ParentId)
            @Html.ValidationMessageFor(model => model.ProgramId)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Name, "名称", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", @maxlength = 50 } })
            @Html.ValidationMessageFor(model => model.Name)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.EType, "类型", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.EType, new List<SelectListItem>()
           {
           new SelectListItem() {Value=(E_Type_Menu.Menu).ToString(),Text="菜单" },
           new SelectListItem() {Value=(E_Type_Menu.Function).ToString(),Text="功能" }
           }, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.EType)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Url, "链接", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Url, new { htmlAttributes = new { @class = "form-control", @maxlength = 50 } })
            @Html.ValidationMessageFor(model => model.Url)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Ordering, "排序值", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Ordering, new { htmlAttributes = new { @class = "form-control", @maxlength = 10 } })
            @Html.ValidationMessageFor(model => model.Ordering)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.EStatus, "状态", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.EStatus, new List<SelectListItem>()
           {
               new SelectListItem() {Value=(E_Status.Disable).ToString(),Text="不可用" },
               new SelectListItem() {Value=(E_Status.Enable).ToString(),Text="可用" }
           }, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.EStatus)
        </div>
    </div>

</div>

<script src="~/bower_components/bootstrap_dropdowns_enhancement-master/dropdowns-enhancement.js"></script>
<script src="~/Scripts/Common/dropdowntree.js"></script>
<script>
    var dropdownajaxurl = "@Url.Action("DropDownData")";
    var parentId = "@Model.ParentId";
    var programId = "@Model.ProgramId";
    var nullpgid = "请选择程序";
    var rootnode = "根节点";
    $(function () {
        var pgid = $("#ProgramId").val();
        dropdownGetData(pgid, parentId);
    });
    //树形下拉获取数据
    function dropdownGetData(pgid,pid)
    {
        if (!pgid) {
            toastr.error(nullpgid);
            return;
        }
        loading.start();
        $("#divPid .dropdown-menu").empty();
        $.ajax({
            url: dropdownajaxurl,
            data: { programId: pgid },
            type: "POST",
            dataType: "JSON",
            success: function (treedata) {
                var d = $.arraytotree(treedata.Data, {
                    id: "Id",
                    text: "Name",
                    parentId: "ParentId",
                });
                var opt = {
                    defaultval: pid,
                    data: d,
                    value: "Id",
                    text: "Name",
                    showall: true,
                    allvalue: "0",
                    alltext: rootnode,
                    sons: "nodes",
                    onlyselleaf: false
                };
                $("#divPid").dropdowntree(opt);

                $("#divPid .leaf").each(function (i, e) {
                    $(e).click(function () {
                        var v = $.fn.dropdowntree.getValue();
                        $("#ParentId").val(v);
                    });
                });
            },
            error: function (xhr, status, error) {
                alert("error");
            },
            complete: function () {
                loading.end();
            }
        });
    }
    //项目修改
    function programChange() {
        var pgid = $("#ProgramId").val();
        dropdownGetData(pgid);
    }
</script>